home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-10-09 | 9.0 KB | 361 lines | [TEXT/MPS ] |
- {*******************************************************************************
- UValidText.inc1.p
- TValidText is a class implementing the ability to enter strings which must
- be validated, such as dates, times, numbers, and so on.
- *******************************************************************************}
-
-
- {###############################################################################
- Global Routines
- ###############################################################################}
-
- {------------------------------------------------------------------------------}
- {$S AInit}
-
- PROCEDURE InitUValidText;
- VAR
- dummy: BOOLEAN;
-
- BEGIN
- IF gDeadStripSuppression
- THEN
- BEGIN
- { view classes }
- dummy := Member(TObject(NIL), TValidText);
- END; { then }
- END; { InitUValidText }
-
-
- {###############################################################################
- TValidText
- ###############################################################################}
-
- {------------------------------------------------------------------------------}
- {$S DlgOpen}
-
- PROCEDURE TValidText.IValidText(
- itsSuperView: TView;
- itsLocation: VPoint;
- itsSize: VPoint;
- strict: BOOLEAN;
- required: BOOLEAN;
- alertID: INTEGER);
-
- BEGIN
- IEditText(itsSuperView, itsLocation, itsSize, 255);
-
- fStrict := strict;
- fRequired := required;
- fAlertID := alertID;
- END; { IValidText }
-
- {------------------------------------------------------------------------------}
- {$S DlgOpen}
-
- PROCEDURE TValidText.IRes(
- itsDocument: TDocument;
- itsSuperView: TView;
- VAR itsParams: Ptr);
- OVERRIDE;
- VAR
- theText: Str255;
-
- BEGIN
- INHERITED IRes(itsDocument, itsSuperView, itsParams);
-
- { read extra field from resource }
- WITH ValidTextTemplatePtr(itsParams)^ DO
- BEGIN
- fStrict := strict;
- fRequired := required;
- fAlertID:= alertID;
- END;
-
- { offset the itsParam ptr accordingly }
- OffsetPtr(itsParams, SIZEOF(ValidTextTemplate));
- END; { IRes }
-
-
- {------------------------------------------------------------------------------}
- {$S MAWriteRes}
-
- PROCEDURE TValidText.WRes(
- theResource: ViewRsrcHndl;
- VAR itsParams: Ptr);
- OVERRIDE;
-
- VAR
- p: ValidTextTemplatePtr;
-
- BEGIN
- INHERITED WRes(theResource, itsParams);
-
- p := ValidTextTemplatePtr(ExpandPtr(theResource,
- itsParams,
- SIZEOF(ValidTextTemplate)));
-
- WITH p^ DO
- BEGIN
- strict := fStrict;
- required := fRequired;
- END;
- END; { WRes }
-
-
- {------------------------------------------------------------------------------}
- {$S MAWriteRes}
-
- PROCEDURE TValidText.WriteRes(
- theResource: ViewRsrcHndl;
- VAR itsParams: Ptr);
- OVERRIDE;
-
- BEGIN
- gWResSignature := 'vTxt'; { See ViewResourceTypes.r }
- gWResType := 'TValidText';
- WRes(theResource, itsParams);
- END; { WriteRes }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- PROCEDURE TValidText.SetStrict(
- strict: BOOLEAN);
-
- BEGIN
- fStrict := strict;
- END; { SetStrict }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- PROCEDURE TValidText.SetRequired(
- required: BOOLEAN);
-
- BEGIN
- fRequired := required;
- END; { SetRequired }
-
- {------------------------------------------------------------------------------}
- {$S DlgRes}
-
- FUNCTION TValidText.GetStrict
- :BOOLEAN;
- BEGIN
- GetStrict := fStrict;
- END; { GetStrict }
-
- {------------------------------------------------------------------------------}
- {$S DlgRes}
-
- FUNCTION TValidText.GetRequired
- :BOOLEAN;
- BEGIN
- GetRequired := fRequired;
- END; { GetRequired }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- PROCEDURE TValidText.UpdateText(
- redraw: BOOLEAN);
- { This routine replaces the text in the object with text that
- reflects the object's current values. }
- BEGIN
- {$IFC qDebug}
- ProgramBreak('The routine TValidText.UpdateText) MUST BE OVERRIDEN!');
- {$ENDC qDebug}
- END; { UpdateText }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.IsValid(
- VAR theText: Str255;
- VAR whyNot: INTEGER)
- :BOOLEAN;
- { If the text is valid, then this function returns TRUE, and whyNot
- is set to the value noErr (0). If the text is invalid, then the
- function returns FALSE and whyNot is set to a value indicating
- the reason why the text is invalid. }
- BEGIN
- IF fRequired &
- (Length(theText) = 0)
- THEN
- BEGIN
- IsValid := FALSE;
- whyNot := kEmptyStringError;
- END
- ELSE
- BEGIN
- IsValid := TRUE;
- whyNot := noErr;
- END;
- END; { IsValid }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.HandleValidText(
- VAR theText: Str255)
- :LONGINT;
- BEGIN
- { tell SELF's superview that SELF's text validated OK }
- fSuperView.DoChoice(SELF, mTextValidated);
-
- HandleValidText := kValidValue;
- END; { HandleValidText }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.HandleInvalidText(
- VAR theText: Str255;
- theError: INTEGER)
- :LONGINT;
- BEGIN
- IF ValidationErrorAlert(theText, theError)
- THEN
- HandleInvalidText := HandleAlertAccepted(theText, theError)
- ELSE
- HandleInvalidText := HandleAlertCancelled(theText, theError);
- END; { HandleInvalidText }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.HandleAlertAccepted(
- VAR theText: Str255;
- theError: INTEGER)
- :LONGINT;
- { This routine always returns kErrorHandled. Its overrides might
- do something different. }
- BEGIN
- HandleAlertAccepted := kErrorHandled;
- END; { HandleAlertAccepted }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.HandleAlertCancelled(
- VAR theText: Str255;
- theError: INTEGER)
- :LONGINT;
- { This routine always returns kErrorHandled. Its overrides might
- do something different. }
- BEGIN
- HandleAlertCancelled := kErrorHandled;
- END; { HandleAlertCancelled }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- PROCEDURE TValidText.ErrorToString(
- theError: INTEGER;
- VAR theString: Str255);
- { This routine sets theString to the string which best explains
- the given error. It is intended to be called only from
- ValidationErrorAlert(). }
- BEGIN
- IF (theError = kEmptyStringError)
- THEN
- GetIndString(theString, kValidTextErrorStrings, kEmptyStringErrorStr)
- ELSE
- GetIndString(theString, kInvalidValueReasons, kInvalidValue);
-
- (*
- {$IFC qDebug}
- writeln;
- write('In TValidText.ErrorToString(), theString = "',
- theString, '" and theError = ');
- WriteHexInt(theError);
- writeln;
- {$ENDC qDebug}
- *)
- END; { ErrorToString }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- PROCEDURE TValidText.PrepareErrorAlert(
- VAR theText: Str255;
- theError: INTEGER);
- { The routine sets up the dialog that is displayed by
- ValidationErrorAlert(), below. }
- VAR
- theString: Str255;
-
- BEGIN
- { get the best string to describe the given error }
- ErrorToString(theError, theString);
- ParamText(theString, '', '', '');
- END; { PrepareErrorAlert }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.ValidationErrorAlert(
- VAR theText: Str255;
- theError: INTEGER)
- : BOOLEAN;
- { This routine displays the standard MacApp validation error alert.
- The function result (true by default) indicates whether or not
- the alert was accepted (true) or cancelled (false). }
- BEGIN
- { Display the alert, and return the result }
- PrepareErrorAlert(theText, theError);
- ValidationErrorAlert := (MacAppAlert(fAlertID, NIL) = ok);
- END; { ValidationErrorAlert }
-
- {------------------------------------------------------------------------------}
- {$S DlgNonRes}
-
- FUNCTION TValidText.Validate
- :LONGINT;
- OVERRIDE;
-
- VAR
- parentResult: LONGINT;
- theText: Str255;
- whyNot: INTEGER;
-
- BEGIN
- { make sure the current text passes the superclass' validation }
- parentResult := INHERITED Validate;
-
- IF (parentResult <> kValidValue)
- THEN
- Validate := parentResult
- ELSE
- BEGIN
- GetText(theText);
-
- IF IsValid(theText, whyNot)
- THEN
- Validate := HandleValidText(theText)
- ELSE
- Validate := HandleInvalidText(theText, whyNot);
- END;
- END; { Validate }
-
- {------------------------------------------------------------------------------}
- {$S DlgFields}
-
- {$IFC qInspector}
- PROCEDURE TValidText.Fields(
- PROCEDURE DoToField(
- fieldName: Str255;
- fieldAddr: Ptr;
- fieldType: INTEGER));
- OVERRIDE;
-
- BEGIN
- DoToField('TValidText', NIL, bClass);
- DoToField('fStrict', @fStrict, bBoolean);
- DoToField('fRequired', @fRequired, bBoolean);
-
- INHERITED Fields(DoToField);
- END; { Fields }
- {$ENDC qDebug}
-
- {------------------------------------------------------------------------------}